﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NanoERP.Business.Permission;
using NanoERP.Model.Poco;
using NanoERP.Infrastructure;

namespace NanoERP.Modules.SystemMgmt
{
    //[Module("UserMgmt")]
    public partial class FrmEditRole : EditFormBase
    {
        private SYS_Role _data;
        public SYS_Role Data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
                this.Mode = this.Enabled ? FormMode.Edit : FormMode.View;
            }
        }

        public Guid? TypeId { get; set; }

        public FrmEditRole()
        {
            InitializeComponent();
        }

        protected override void LoadData()
        {
            this.Id = Data.Id;
            //this.longinNameTextBox.Text = Data.LoginName;
            this.descriptionTextBox.Text = Data.Remark;
            this.nameTextBox.Text = Data.Name;
            this.sortTextBox.Text = Data.Sort.ToString();
        }

        protected override bool SaveData()
        {
            var srv = new RoleService();

            SYS_Role user = !this.Id.Equals(Guid.Empty) ? srv.GetRole(this.Id) : new SYS_Role()
                                    {
                                        Id = Guid.NewGuid(),
                                        TypeId = TypeId
                                    };

            //user.LoginName = this.longinNameTextBox.Text;
            user.Remark = this.descriptionTextBox.Text;
            user.Name = this.nameTextBox.Text;
            user.Sort = Int32.Parse(this.sortTextBox.Text);

            if (this.Id.Equals(Guid.Empty))
            {
                srv.AddRole(user);
            }
            else
            {
                srv.UpdateRole(user);
            }
            return true;
        }

    }
}
